package com.bangbang.controller;

import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.pojo.PageEsVO;
import com.bangbang.service.EsService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("esController")
public class EsController {

    @Autowired
    private EsService esService;

    @GetMapping
    @ApiOperation("es查询&&网页的查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "搜索的关键字",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNumber",value = "当前页数",required = false,paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页展示的条数",required = false,paramType = "query"),
    })
    public Result select(@RequestParam("name")String name, @RequestParam(value = "pageNumber",required = false,defaultValue = "1" )Integer pageNumber, @RequestParam(value = "size",defaultValue = "5" ,required = false)Integer size)throws IOException {

        PageEsVO pec = esService.select(name,pageNumber,size);
        if(pec != null){
            Map<String ,Object> map = new HashMap<>();
            map.put("pageNumber",pageNumber);
            map.put("list",pec.getList());
//        总条数
            Integer totalCount = pec.getCount().intValue();
//       总页数
            Integer zys =  totalCount % size == 0 ? totalCount / size : totalCount / size + 1;
            map.put("zys",zys);
            if(pec.getList() == null || pec.getList().size() <= 0){
                return Result.buildResult(ResultEnum.ERROR);
            }
            return Result.buildResult(ResultEnum.SUCCESS).setResult(map);
        }else {
            return Result.buildResult(ResultEnum.ERROR);
        }

    }
}
